package com.gzbugu.polytechsasia.service.impl;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gzbugu.polytechsasia.factory.DAOFactory;
import com.gzbugu.polytechsasia.service.ILoginService;
import com.gzbugu.polytechsasia.vo.User;

/**
 * Login Service
 * 
 * @author gauze
 * 
 */
public class LoginService implements ILoginService {

	private static Logger log = LoggerFactory.getLogger(LoginService.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.gzbugu.polytechsasia.service.impl.ILoginService#isValidate(java.lang
	 * .String, java.lang.String)
	 */
	public boolean isValidate(String passport, String password) {
		log.debug("check passport and password, passport: " + passport);
		try {
			if (passport == null || password == null) {
				log.debug("passport or password is null!");
				return false;
			}
			User user = DAOFactory.getUserDAO().findByPassport(passport.trim());
			if (user == null) {
				log.debug("passport is not exist!");
				return false;
			}
			log.debug("passport is validate!");
			if (user.getPassword().equals(password)) {
				log.debug("password is validate!");
				return true;
			}
			log.debug("password is false!");
			return false;
		} catch (SQLException e) {
			log.error("check failed and return false", e);
			return false;
		}
	}

}
